package com.example.miaosha.mapper;

import com.example.miaosha.entity.Item;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * Created by lerry_li on 2020/10/20
 */
@Mapper
public interface ItemMapper {
    //通过id获取商品信息
    @Select("SELECT * FROM item WHERE id = #{id}")
    Item selectByPrimaryId(Integer id);

    //获得所有商品，按销量排序
    @Select("SELECT * FROM item ORDER BY sales DESC")
    List<Item> listItem();

    //插入一条记录
    @Insert("INSERT INTO item (title,price,description,img_url)" +
            "VALUES (#{title},#{price},#{description},#{imgURL})")
    void insert(Item item);

    @Update("Update item SET sales = sales + #{amount} " +
            "WHERE id = #{itemId}")
    void increaseSales(Integer itemId,Integer amount);
}
